home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- ** **
- ** trace **
- ** **
- ** void Trace(char *format, ...) **
- ** **
- ** It is not possible to use printf in a Windows library **
- ** because DS != SS. This procedure provides a simple alternative. **
- ** **
- ** The format string is not quite as for printf. The following escapes **
- ** are used: **
- ** **
- ** %c - A character **
- ** %d - A decimal number **
- ** %u - An unsigned number **
- ** %s - A string **
- ** %b - A 2 digit hex number **
- ** %w - A 4 digit hex number **
- ** %p - A far pointer (XXXX:YYYY) **
- ** No field widths are supported. All output is sent to stdout. **
- ** **
- ** Written by D. G. Booth 5th February 1987 **
- ** **
- ** Copyright (C) Richards Computer Products Ltd. 1987 **
- ** **
- *************************************************************************/
- #include <stdio.h>
- #include <dos.h>
- #include "types.h"
- #include "trace.h"
-
- /* Copy of stdarg.h fudged to work with FAR pointers! */
- typedef char far *va_list;
-
- #define va_start(ap,v) ap = (va_list)&v + sizeof(v)
- #define va_arg(ap,t) ((t far *)(ap += sizeof(t)))[-1]
- #define va_end(ap) ap = NULL
-
- void trace(LPSTR, ...);
- void pascal vTrace(LPSTR, WORD far *);
- void OutCh(WORD);
-
- /* This routine avoids loading too many C runtime routines: */
- #define prch(c) OutCh(c)
-
- static char HexString[] = "0123456789abcdef";
-
- static void prunsig(u)
- register unsigned int u;
- {
- if (u > 9) prunsig(u/10);
- prch(u%10 + '0');
- }
-
- static void prdec(n)
- register int n;
- {
- if (n < 0)
- {
- prch('-'); n = -n;
- }
- prunsig((unsigned int) n);
- }
-
- #define prnibl(n) prch (HexString [n & 0x0f])
-
- static void prbyte(b)
- WORD b;
- {
- prnibl (b >> 4); prnibl(b);
- }
-
- static void prword(w)
- WORD w;
- {
- prbyte(w >> 8); prbyte(w);
- }
-
- void trace(s, t)
- LPSTR s;
- WORD t;
- {
- return;
- vTrace ((LPSTR) s, (WORD far *) &t);
- }
-
- void pascal vTrace (format, args)
- LPSTR format;
- WORD far *args;
- {
- while (*format)
- {
- if (*format == '%') switch(*++format)
- {
- case 'p': /* Far pointer */
- {
- prword (args[1]); prch(':'); prword(args[0]);
- args += 2;
- break;
- }
- case 'c': prch (*args++); break;
- case 'd': prdec ((int) *args++); break;
- case 'u': prunsig(*args++); break;
- case 'b': prbyte (*args++); break;
- case 'w': prword (*args++); break;
- case 's':
- {
- LPSTR t = * ((LPSTR far *) args)++;
- if (t == NULL) vTrace("(null)", NULL);
- else while(*t)
- {
- if (*t == '\n') prch('\r');
- prch(*t++);
- }
- break;
- }
- default:
- prch(*format);
- break;
- } else if (*format == '\n') {
- prch('\r');
- prch('\n');
- } else
- prch(*format);
- format++;
- }
- }
-